package com.itheima.web.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/resp1")
public class ResponseDemo1 extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("resp1.......");

        //重定向
       /* //设置响应状态码
        response.setStatus(302);
        //设置响应头"location
        response.setHeader("location","/tomcat-demo/resp2");*/

       //简化方式完成重定向
//        response.sendRedirect("/tomcat-demo/resp2");
//        response.sendRedirect("https://www.itcast.cn");

        //动态获取虚拟目录
//        String contextPath = request.getContextPath();
//        response.sendRedirect(contextPath+"/resp2");

        response.sendRedirect(request.getContextPath()+"/resp2");

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }


}
